
#include  "head.h"
#include <sqlite3.h>

int ttid;
struct datas
{
    sqlite3 *db;
    pthread_t tidd;
    struct sockaddr_in  useraddr;
    int connfd;
};
int count;
void *fun(void *x)
{
    struct datas *data = (struct datas*)x;
    while(1)
    {
        char *errmsg = NULL;
        char sql[128] = {0};
        char **result = 0;
        int col = 0,row = 0;
        char buff[32]={0};
        int ret = recv(data->connfd,buff,sizeof(buff),0);
        if(ret <= 0)
        {
            perror("recv error");
            ttid = data->tidd;
            pthread_exit(NULL);
        }
        if(!strcmp(buff,"[]\r\n"))
        {
            ttid = data->tidd;
            close(data->connfd);
            //printf("ttid =%d\n",ttid);
            pthread_exit(NULL);
        }
        sprintf(sql,"select translate from dict where word = '%s';",buff);
        ret = sqlite3_get_table(data->db,sql,&result,&row,&col,&errmsg);
        if(ret < 0)
        {
            perror("sqlite3_get_table");
            return NULL;
        }
        /*for(int i = 0;i<=row;i++)
        {
            for(int j = 0;j<col;j++)
            {
                printf("%s ",result[i*row+j]);
            }
            printf("\n");
        }*/
        //printf("trans:%d  %d\n",row,col);
        //printf("trans:%s\n",result[1]);
        char buf[512] ={0};
        if(col == 0)       send(data->connfd,"no such word",12,0);
        else{
            strcpy(buf,result[1]);
            send(data->connfd,buf,sizeof(buf),0);
            printf("recv:%s\n",buff);
            
        }
    }
}
void test(int sig)
{
    count--;
    pthread_join(ttid,NULL);
}
int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("Usage %s server ip  port\n",argv[0]);
        return -1;
    }


    int sockfd  = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd < 0)
    {
        perror("socket error");
        return -1;
    }
    printf("sockfd = %d \n",sockfd);


    struct sockaddr_in  localaddr;
    socklen_t   len  =  sizeof(struct sockaddr_in);   
    localaddr.sin_family            = AF_INET;
    localaddr.sin_port              = htons(atoi(argv[2]));
    localaddr.sin_addr.s_addr       = inet_addr(argv[1]);



    int on =1 ;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
    int ret = bind(sockfd, (struct sockaddr*)&localaddr,len);
    if(ret < 0)
    {
        perror("bind error");
        return -1;
    }
    printf("bind ok \n");

    struct datas data;  //结构体
    ret = sqlite3_open("./dict.db",&data.db);
    if(ret < 0)
    {
        perror("sqlite3_open error");
        return -1;
    }

    listen(sockfd,1);
    int sig;
    struct sockaddr_in  usaddr[128];
    pthread_t tid[128]  = {0};
    count = 0;
    int confd[128] = {0};
    while(count < 128)
    {
        signal(SIGUSR1,test);
        confd[count] = accept(sockfd,(struct sockaddr*)&usaddr[count],&len);
        if(confd[count] <= 0)
        {
            perror("accept error");
            return -1;
        }
        if(confd > 0) 
        {
            data.tidd = tid[count];
            data.useraddr.sin_family = usaddr[count].sin_family;
            data.useraddr.sin_addr.s_addr = inet_addr(inet_ntoa(usaddr[count].sin_addr));
            data.useraddr.sin_port = usaddr[count].sin_port;
            data.connfd = confd[count];
            pthread_create(&tid[count],NULL,fun,&data);
            printf("该线程tid =%lu\n",tid[count]);
            count++;
        }
    }
    close(sockfd);
    return 0;
}
